<?php
namespace app\common\model;

use think\model\concern\SoftDelete;

class User extends Base
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    
    /**
     * 启用||禁用
     */
    public static function setStatus($id){
        $info = self::getFind(['id' => $id]);

        if($info['status'] == 0){
            $status       = 1;
            $back['flag'] = 1;
            $back['msg']  = '启用成功';
        }else{
            $status       = 0;
            $back['flag'] = 0;
            $back['msg']  = '禁用成功';
        }

        $data = [
            'id'      => $id,
            'status'  => $status,
        ];
        
        self::update($data);
        return $back;
    }

    public function getPidAttr($val)
    {
        if($val == 0) return '';
        return self::getVal(['id' => $val], 'nickname');
    }

    public function getStarAttr($val)
    {
        if($val == 0) return '普通会员';
        return Star::getVal(['id' => $val], 'name');
    }

    public function getAvatarAttr($val)
    {
        if(stripos($val, 'thirdwx.qlogo.cn') !== false) return $val;
        return getUrl() . $val;
    }

    /**
     * 更新会员等级
     */
    public static function reckonGrade($user_id = 0)
    {
        if($user_id == 0) return true;
        $userInfo = self::where('id', $user_id)->find();
        if($userInfo === false) return false;

        /** 直推人数 */
        $pushNums = self::getPushNums($user_id);

        /** 直推同级人数 */
        $pushSameLevelNums = self::getPushSameLevelNums($user_id);

        /** 查询星级列表 */
        $starList = Star::where([['grade', '>', $userInfo->getData('star')]])->order('grade')->select();

        if($starList->isEmpty()) return false;

        foreach($starList as $v){
            if($userInfo['consume_brokerage'] < $v['consume'] 
            || $pushNums < $v['push_num'] 
            || $pushSameLevelNums < $v['push_same_level_num'])
            {
                continue;
            }

            /** 更新会员等级 */
            self::setIncField(['id' => $user_id], 'star');
        }

        return true;
    }

    /**
     * 发放推荐奖
     */
    public static function pushAward($user_id, $total, $order_id)
    {
        $system = getSystemVal();
        $userInfo = self::where('id', $user_id)->find();

        if(!$userInfo['pid']) return false;

        /** 用户直接推荐人信息 */
        $userSecInfo = self::where('id', $userInfo['pid'])->find();

        if(!$userSecInfo) return false;
        if($userInfo->getData('star') < $userSecInfo->getData['star']) return false;

        /** 直推奖 */
        $pushAward = $system['push_reward'] * $total;

        if($pushAward > 0){
            /** 增加佣金 */
            self::setIncField(['id' => $userSecInfo['id']], 'money_brokerage', $pushAward);

            /** 写入财务记录 */
            FinancialRecord::adds($userSecInfo['id'], $order_id, 2, 1, '培育会员'. $userSecInfo['name'] .'佣金收入', $pushAward);
        }

        return true;
    }

    /**
     * 查询直推人数
     */
    public static function getPushNums($uid = 0)
    {
        return self::where('pid', $uid)->count();
    }

    /**
     * 查询直推同级人数
     */
    public static function getPushSameLevelNums($uid = 0)
    {
        return self::where(['pid' => $uid, 'star' => self::where('id', $uid)->value('star')])->count();
    }
}